/*
 * Test glMatrix using qunit.js/qunit.css.
 */
 
var prec = 0.000001;
 
var  comparer = function(test1,test2) {
    for (i = 0 ; i < test1.length; i++) {
                if (Math.abs( test1[i] - test2[i] ) > prec ) {
                        return false;
                }
    }           
    return true;
};
var glMatrix_runtests = function() {

 var WebGLFloatArray = window.WebGLFloatArray || undefined;
 var Float32Array = window.Float32Array || undefined;
 var Array = window.Array ||undefined; 
 
 var types2 = [Array ,Float32Array,WebGLFloatArray];
 var typestext = ["Array", "Float32Array","WebGLFloatArray"];
  for(var i = 0;i<types2.length; i++){
    if (types2[i] === undefined){
    continue;
    };
  glMatrixArrayType = types2[i];

    module("Test glMatrix routines for " + typestext[i] +  " : ");

  test("vec3",function () {

    var v1 = vec3.create();

    ok(v1,"vec3.create() has create a vec3");
    ok(comparer(v1,[0,0,0]),"vec3.create() creates a empty vec3");

    var v2 = vec3.create([1,2,3]);
        ok(comparer(v2,[1,2,3]),"dest = vec3.create(array) has create a vec3 with specific content");
        
    v1 = vec3.set([4,5,6],v2);
        ok(comparer(v2,[4,5,6]),"vec3.set(array,dest) has set the dest with the correct content");
        ok(comparer(v1,[4,5,6]),"dest = vec3.set(array) has set the array and dest with the correct content");
        
        v1 = vec3.add([1,2,3],[3,3,3],v2);
        ok(comparer(v2,[4,5,6]),"vec3.add(array1,array2,dest) has added array1 and array2 and return the vec3 as dest");
        ok(comparer(v1,[4,5,6]),"dest = vec3.add(array1,array2)");
        
        v1 = vec3.subtract([5,7,9],[1,2,3],v2);
        ok(comparer(v2,[4,5,6]),"vec3.subtract(array1,array2,dest)");
        ok(comparer(v1,[4,5,6]),"dest = vec3.subtract(array1,array2)");

        v1 = vec3.negate([-4,-5,-6],v2);
        ok(comparer(v2,[4,5,6]),"vec3.negate(array1,array2,dest)");
        ok(comparer(v1,[4,5,6]),"dest = vec3.negate(array1,array2)");
        
        v1 = vec3.scale([4,5,6],3,v2);
        ok(comparer(v2,[12,15,18]),"vec3.scale(array1,dest)");
        ok(comparer(v1,[12,15,18]),"dest = vec3.scale(array1)");
        
        v1 = vec3.normalize([0,6,8],v2);
        ok(comparer(v2,[0,0.6,0.8]),"vec3.normalize(array1,dest)");
        ok(comparer(v1,[0,0.6,0.8]),"dest = vec3.normalize(array1)");
        
        v1 = vec3.cross([4,5,6],[1,2,3],v2);
        ok(comparer(v2,[3,-6,3]),"vec3.cross(array1,array2,dest)");
        ok(comparer(v1,[3,-6,3]),"dest = vec3.cross(array1,array2)");

        ok(vec3.length([0,6,8])===10,"vec3.length(vec1)");
        
        ok(vec3.dot([1,2,3],[4,5,6])===32,"vec3.dot(vec1,vec2)")
        
        v1 = vec3.direction([4,5,6],[1,2,3],v2);
        ok(comparer(v2,[0.57735, 0.57735, 0.57735]),"vec3.direction(array1,array2,dest)");
        ok(comparer(v1,[0.57735, 0.57735, 0.57735]),"dest = vec3.direction(array1,array2)");

        ok(vec3.str([1,2,3]) === "[1, 2, 3]","vec3.str(vec1)");
        
  });

  test("mat3",function () {
  
        var m1 = mat3.create();
        
        ok(m1,"mat3.create()");
    ok(comparer(m1,[0,0,0,0,0,0,0,0,0]),"mat3.create()");

        var m2 = mat3.create([1,2,3,4,5,6,7,8,9]); 
        ok(comparer(m2,[1,2,3,4,5,6,7,8,9]),"dest = mat3.create(mat)");
                
        m1 = mat3.set([9,8,7,6,5,4,3,2,1],m2);
        ok(comparer(m2,[9,8,7,6,5,4,3,2,1]),"mat3.set(mat,dest)");
        ok(comparer(m1,[9,8,7,6,5,4,3,2,1]),"dest = mat3.set(mat)");
        
        m1 = mat3.identity(m2);
        ok(comparer(m2,[1,0,0,0,1,0,0,0,1]),"mat3.identity(dest)");
        ok(comparer(m1,[1,0,0,0,1,0,0,0,1]),"dest2 = mat3.identity(dest1)");
        
        var m3 = mat4.create()
        m1 = mat3.toMat4([1,2,3,4,5,6,7,8,9],m3);
        ok(comparer(m3,[1,2,3,0,4,5,6,0,7,8,9,0,0,0,0,1]),"mat3.toMat4(mat,dest)");
        ok(comparer(m1,[1,2,3,0,4,5,6,0,7,8,9,0,0,0,0,1]),"dest = mat3.toMat4(mat)");
        
        ok(mat3.str([1,2,3,4,5,6,7,8,9]) === "[1, 2, 3, 4, 5, 6, 7, 8, 9]","mat3.str(mat)");
  });

  test("mat4",function () {

    var i,j;

    var m1 = mat4.create();

    var ident = mat4.identity(m1);
    ok(ident,"identity matrix");

    function testIdentity(m) {
      for (i = 0 ; i < 4 ; i++) {
        for (j = 0 ; j < 4 ; j++) {
          if (m[i + 4*j] !=(i==j ? 1.0 : 0.0) )
            return false;
        }
        return true;
    }
      
      
    }

    ok(testIdentity(ident),"identity matrix");

    try {
      mat4.lookAt([0,0,0],[0,0,0],[0,1,0],m1)
    } catch (e) {
      ok(false, "we shouldn't have died here");
    }});


  test("quat4",function () {

  });
}
    };
